// pages/user/userData/userData.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        editShow: false,
        editInfo: '',
        editType: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function(e) {})
    },

    goMyAddress: function() {
        wx.navigateTo({
            url: '/pages/user/address/addressList/addressList'
        })
    },

    goMyInviter: function() {
        if (this.data.userInfo.parent_phone_c == 0) {
            wx.showToast({
                title: '暂无邀请人',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.makePhoneCall({
            phoneNumber: this.data.userInfo.parent_phone_c
        })
    },

    goMyCard: function() {
        wx.navigateTo({
            url: '/pages/user/userCard/userCard'
        })
    },

    goBrowseHistory: function() {
        wx.navigateTo({
            url: '/pages/user/user_browse/user_browse'
        })
    },

    editData: function(e) {
        this.setData({
            editShow: true,
            editType: e.currentTarget.dataset.type,
            editInfo: e.currentTarget.dataset.info
        })
    },

    editInfoInput: function(e) {
        this.setData({
            editInfo: e.detail.value
        })
    },

    cancleEdit: function() {
        this.setData({
            editShow: false,
            editInfo: '',
            editType: null
        })
    },

    sureEdit: function() {
        if (this.data.editInfo == '') {
            wx.showToast({
                title: '请填写信息',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data
        if (this.data.editType == 'name') {
            data = {
                nickname: this.data.editInfo
            }
        } else if (this.data.editType == 'wx') {
            data = {
                wx_id: this.data.editInfo
            }
        }
        common.post('User/up_user', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '保存成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.getUserInfo()
                        that.setData({
                            editShow: false,
                            editInfo: '',
                            editType: null
                        })
                    }
                }
            })
        }).catch(function(e) {})
    },

    changeHeader: function() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths[0];
                wx.navigateTo({
                    url: '/pages/cropper/cropper?type=3&src=' + tempFilePaths + '&ratio=true&width=240&height=240'
                })
            }
        })
    }
})